//
//  UserAccount.swift
//  Weibo
//
//  Created by 鹏 周 on 15/12/13.
//  Copyright © 2015年 ycit. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            //当过期时间一旦被设置的时候  就立即设置expires_date\
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }

    //token的过期日期  开发者账号的token的有效期 是 5年  普通用户的账号有效期 是 3天
    var expires_date: NSDate?
    
    var uid: String?
    
    var name: String?
    
    var avatar_large: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
//        print("\(key)不是指定的key")
       
    }
    //只读属性 也叫计算型属性
    override var description: String {
        let keys = ["access_token","expires_in","uid","name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }

//    func saveAccount() {
//    let path = ((NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
//    print(path)
//        
//    NSKeyedArchiver.archiveRootObject(self, toFile: path)
//     
//    }
    
    //class func  表示的是类方法
//    class func loadAccount() -> UserAccount? {
//    
//    
//    
//    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeDouble(expires_in,forKey: "expires_in")
    
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        
        
        
        
    }
    
}
